package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.AssetsMapper;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.mapper.UserMapper;
import com.yl.applicationsecurity.pojo.*;
import com.yl.applicationsecurity.service.AssetsService;
import com.yl.applicationsecurity.service.UserService;
import com.yl.applicationsecurity.utils.Md5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AssetsServiceImpl extends ServiceImpl<AssetsMapper, Assets> implements AssetsService {
    @Autowired
    private ScriptStateMapper scriptStateMapper;
    @Override
    public PageInfo<Assets> findListByAssets(String[] nationSelect, Assets assets, Date[] startAndEndFirstDate, Date[] startAndEndLastDate, Integer pageNum, Integer pageSize) {


        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<Assets> lambdaQueryWrapper = new LambdaQueryWrapper<>();




        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(Assets::getNation,nationSelect);
        }

        if (StringUtils.isNotEmpty(assets.getDomain())){
            lambdaQueryWrapper.like(Assets::getDomain,assets.getDomain());
        }


        if (null!=assets.getType()){
            lambdaQueryWrapper.eq(Assets::getType,assets.getType());
        }

        if (null!=assets.getIsWaf()){
            lambdaQueryWrapper.eq(Assets::getIsWaf,assets.getIsWaf());
        }

        if (StringUtils.isNotEmpty(assets.getStatusCode())){
            lambdaQueryWrapper.eq(Assets::getStatusCode,assets.getStatusCode());
        }
        if (StringUtils.isNotEmpty(assets.getTag())){
            lambdaQueryWrapper.like(Assets::getTag,assets.getTag());
        }

        if(startAndEndLastDate.length!=0&&startAndEndLastDate[0]!=null){
            lambdaQueryWrapper.ge(Assets::getLastDate, startAndEndLastDate[0]);
        }

        if(startAndEndLastDate.length!=0&&startAndEndLastDate[1]!=null){
            lambdaQueryWrapper.le(Assets::getLastDate, startAndEndLastDate[1]);
        }

        if(startAndEndFirstDate.length!=0&&startAndEndFirstDate[0]!=null){
            lambdaQueryWrapper.ge(Assets::getFirstDate, startAndEndFirstDate[0]);
        }


        if(startAndEndFirstDate.length!=0&&startAndEndFirstDate[1]!=null){
            lambdaQueryWrapper.le(Assets::getFirstDate, startAndEndFirstDate[1]);
        }

        lambdaQueryWrapper.orderByDesc(Assets::getFirstDate);//根据首次发现时间排序


        List<Assets> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<Assets> pageInfo = new PageInfo<Assets>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;


    }

    @Override
    public int updateNation(String[] ids, String nation) {
        if(ids==null||ids.length==0||StringUtils.isEmpty(nation)){
            return 1;
        }
        LambdaUpdateWrapper<Assets> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Assets::getNation,nation).in(Assets::getId , ids);
        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int insert(Assets assets) {


        //1. 查询此条资产在数据库中是否存在
        LambdaQueryWrapper<Assets> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(assets.getDomain())){
            lambdaQueryWrapper.in(Assets::getDomain,assets.getDomain());
        }
        Long l = baseMapper.selectCount(lambdaQueryWrapper);
        if(l>0){
            Assets assets2 = baseMapper.selectOne(lambdaQueryWrapper);



            LambdaUpdateWrapper<Assets> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

            if(StringUtils.isNotEmpty(assets.getSource())){
                lambdaUpdateWrapper.set(Assets::getSource,assets.getSource());
            }
            if(assets.getIsWaf()!=null&&assets2.getIsWaf()!=2) {
                //waf接入的状态不为无需接入时才会修改
                lambdaUpdateWrapper.set(Assets::getIsWaf, assets.getIsWaf());
            }
               //只有首次发现资产的时候，才有雷达判断资产类型，后续人工可以调整
//            if(assets.getType()!=null) {
//                lambdaUpdateWrapper.set(Assets::getType, assets.getType());
//            }
            if(StringUtils.isNotEmpty(assets.getTitle())) {
                lambdaUpdateWrapper.set(Assets::getTitle, assets.getTitle());
            }
            if(StringUtils.isNotEmpty(assets.getStatusCode())) {
                lambdaUpdateWrapper.set(Assets::getStatusCode, assets.getStatusCode());
            }
            if(StringUtils.isNotEmpty(assets.getNation())) {
                lambdaUpdateWrapper.set(Assets::getNation, assets.getNation());
            }

            lambdaUpdateWrapper.set(Assets::getLastDate,new Date());

            lambdaUpdateWrapper.eq(Assets::getId,assets2.getId());


            return baseMapper.update(null,lambdaUpdateWrapper);
        }else{
            assets.setId(UUID.randomUUID().toString());
            assets.setLastDate(new Date());
            assets.setFirstDate(new Date());
            return baseMapper.insert(assets);
        }

    }


}
